<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <%- include('../layout/common-css.html');%>
    <!-- jsoneditor -->
    <link href="/jsoneditor/jsoneditor.min.css" rel="stylesheet" type="text/css">
    <script src="/jsoneditor/jsoneditor.min.js"></script>
    <title><%if(id){%>修改<%}else{%>增加<%}%> <%=title%></title>
    <style type="text/css">
        #jsoneditor {
            width: 100%;
            height: 500px;
        }
    </style>
</head>
<body>
<%- include('../layout/header.html');%>
<div class="am-panel am-panel-default">
        <div class="am-panel-hd"><h3 class="am-panel-title"><%if(id){%>修改<%}else{%>新增<%}%> <%=title%></h3></div>
        <div class="am-panel-bd">
            <form method="post" class="am-form" action="<%=uri_prefix%>update<%if(id){%>?id=<%=id%><%}%>" id="form" onsubmit="return checkform()">
                <div class="am-form-group">
                    <input type="button" class="am-btn am-btn-success  am-btn-lg" name="back" value="返回" onclick="window.history.back();"/>
                </div>
                <textarea id="jsondata" name="jsondata" style="display:none"><%=JSON.stringify(doc)%></textarea>
                <div class="am-form-group" id="jsoneditor"></div>
                <div class="am-form-group">
                    <input type="submit" class="am-btn am-btn-danger am-btn-lg" name="submit" id="submit" value="提交"/>
                </div>
            </form>
            <script>
                var container = document.getElementById('jsoneditor');
                var datanode = document.getElementById('jsondata');

                var options = {
                    mode: 'tree',
                    modes: ['code', 'form', 'tree', 'view'], // allowed modes
                    error: function (err) {
                        alert(err.toString());
                    }
                };

                var val = JSON.parse(datanode.value);
                delete val['_id'];
                var editor = new JSONEditor(container, options, val);
                editor.expandAll();

                var checkform = function(){
                    try{
                        var jsonvalue = JSON.stringify(editor.get());
                        datanode.value = jsonvalue;
                    }catch(e){
                        return false;
                    }
                    return true;
                }
            </script>
        </div>
</div>
<%- include('../layout/bottom-js.html');%>
</body>
</html>